import config from "../../utils/config";
import {request} from "../../utils/request";
const app = getApp()
Page({
    data: {
        cart_id:null,
        good:{},
        custom:wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        msg:'',
        address: {},
        user:{},
        total:0.00
    },
    onLoad(option) {
        var user = wx.getStorageSync('userInfo')
        this.setData({user})
        this.setData({cart_id:option.cart_id.split('-')})
        this.getGoodInfo()
        this.getAddress()
    },
    getGoodInfo(){
        request({
            url:"/miniprogram/order/getCartInfo",
            data:{cart_id:this.data.cart_id},
            type:'post'
        }).then(res=>{
            this.setData({good:res.data.brandList,total:res.data.total})
        })
    },
    getAddress(){
        var user = wx.getStorageSync('userInfo')
        request({
            url:"/miniprogram/user/getAddress",
            data:{user_id:user.id}
        }).then(res=>{
            if(res.code != 200){
                this.setData({address:false})
            }else{
                this.setData({address:res.data})
            }
        })
    },
    changeValue(e){
        var i = e.currentTarget.dataset.index
        var good = this.data.good
        good[i].msg = e.detail.value
        this.setData({good})
    },
    createOrder(){
        // request({url:"/miniprogram/order/cancelOrder",data:{out_trade_no:'wx1220220908114432469'}})
        // return false;
        wx.showLoading()
        request({
            // url:'/miniprogram/order/createOrder',
            url:'/miniprogram/lklpay/createOrder',
            data:{
                cart_id:this.data.cart_id,
                openid:this.data.user.openid,
                nickname:this.data.user.nickname,
                user_id:this.data.user.id,
                total:this.data.total,
                good:this.data.good,
                user_address_id:this.data.address.id
            },
            type:'post'
        }).then(res=>{
            wx.hideLoading()
            if(res.code !== 200){
                config.toast(res.msg,res.code,()=>{})
                return false;
            }
            // wx.requestPayment({
            //     timeStamp: res.data.timeStamp,
            //     nonceStr: res.data.nonceStr,
            //     package: res.data.package,
            //     signType: res.data.signType,
            //     paySign: res.data.paySign,
            //     success(rel) {
            //         setTimeout(()=>{
            //             request({
            //                 url:"/miniprogram/order/queryOrder",
            //                 data:{out_trade_no:res.data.out_trade_no}
            //             }).then(res2=>{
            //                 config.toast(res2.msg,res2.code,()=>{
            //                     if(res2.code == 200){
            //                         setTimeout(() => {
            //                             wx.navigateTo({url: "/pages/pay_success/pay_success"})
            //                         }, 1500)
            //                     } else {
            //                         setTimeout(() => {
            //                             wx.navigateTo({url: "/pages/my_order_buy/my_order_buy"})
            //                         }, 1500)
            //                     }
            //                 })
            //             })
            //         },1500)
            //     },
            //     fail(err) {
            //         wx.hideLoading()
            //         wx.showToast({ title: '取消支付', icon: 'none' })
            //         request({url:"/miniprogram/order/cancelOrder",data:{out_trade_no:res.data.out_trade_no}})
            //         setTimeout(()=>{
            //             wx.navigateTo({url:"/pages/my_order_buy/my_order_buy"})
            //         },1500)
            //     }
            // })
            wx.requestPayment({
                timeStamp: res.data.acc_resp_fields.time_stamp,
                nonceStr: res.data.acc_resp_fields.nonce_str,
                package: res.data.acc_resp_fields.package,
                signType: res.data.acc_resp_fields.sign_type,
                paySign: res.data.acc_resp_fields.pay_sign,
                success(rel) {
                    setTimeout(()=>{
                        request({
                            url:"/miniprogram/lklpay/queryOrder",
                            data:{out_trade_no:res.data.out_trade_no}
                        }).then(res2=>{
                            config.toast(res2.msg,201,()=>{
                                if(res2.code == 200){
                                    setTimeout(() => {
                                        wx.navigateTo({url: "/pages/pay_success/pay_success"})
                                    }, 1500)
                                } else {
                                    setTimeout(() => {
                                        wx.navigateTo({url: "/pages/my_order_buy/my_order_buy"})
                                    }, 1500)
                                }
                            })
                        })
                    },1500)
                },
                fail(err) {
                    wx.hideLoading()
                    wx.showToast({ title: '取消支付', icon: 'none' })
                    request({url:"/miniprogram/order/cancelOrder",data:{out_trade_no:res.data.out_trade_no}})
                    setTimeout(()=>{
                        wx.navigateTo({url:"/pages/my_order_buy/my_order_buy"})
                    },1500)
                }
            })
        })
    }
})